<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.14"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Paho Asynchronous MQTT C Client Library: Subscription example</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="pahologo.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Paho Asynchronous MQTT C Client Library
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.14 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Subscription example </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;stdio.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;stdlib.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;string.h&gt;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="_m_q_t_t_async_8h.html">MQTTAsync.h</a>&quot;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#define ADDRESS     &quot;tcp://localhost:1883&quot;</span></div><div class="line"><span class="preprocessor">#define CLIENTID    &quot;ExampleClientSub&quot;</span></div><div class="line"><span class="preprocessor">#define TOPIC       &quot;MQTT Examples&quot;</span></div><div class="line"><span class="preprocessor">#define PAYLOAD     &quot;Hello World!&quot;</span></div><div class="line"><span class="preprocessor">#define QOS         1</span></div><div class="line"><span class="preprocessor">#define TIMEOUT     10000L</span></div><div class="line"></div><div class="line"><span class="keyword">volatile</span> <a class="code" href="_m_q_t_t_async_8h.html#a7ca6d2a1813f2bbd0bc3af2771e46ba4">MQTTAsync_token</a> deliveredtoken;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> disc_finished = 0;</div><div class="line"><span class="keywordtype">int</span> subscribed = 0;</div><div class="line"><span class="keywordtype">int</span> finished = 0;</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> connlost(<span class="keywordtype">void</span> *context, <span class="keywordtype">char</span> *cause)</div><div class="line">{</div><div class="line">        <a class="code" href="_m_q_t_t_async_8h.html#a0db1d736cdc0c864fe41abb3afd605bd">MQTTAsync</a> client = (<a class="code" href="_m_q_t_t_async_8h.html#a0db1d736cdc0c864fe41abb3afd605bd">MQTTAsync</a>)context;</div><div class="line">        <a class="code" href="struct_m_q_t_t_async__connect_options.html">MQTTAsync_connectOptions</a> conn_opts = <a class="code" href="_m_q_t_t_async_8h.html#ae18b51f22784a43803eb809d6a0c2492">MQTTAsync_connectOptions_initializer</a>;</div><div class="line">        <span class="keywordtype">int</span> rc;</div><div class="line"></div><div class="line">        printf(<span class="stringliteral">&quot;\nConnection lost\n&quot;</span>);</div><div class="line">        printf(<span class="stringliteral">&quot;     cause: %s\n&quot;</span>, cause);</div><div class="line"></div><div class="line">        printf(<span class="stringliteral">&quot;Reconnecting\n&quot;</span>);</div><div class="line">        conn_opts.<a class="code" href="struct_m_q_t_t_async__connect_options.html#ac8dd0930672a9c7d71fc645aa1f0521d">keepAliveInterval</a> = 20;</div><div class="line">        conn_opts.<a class="code" href="struct_m_q_t_t_async__connect_options.html#a036c36a2a4d3a3ffae9ab4dd8b3e7f7b">cleansession</a> = 1;</div><div class="line">        <span class="keywordflow">if</span> ((rc = <a class="code" href="_m_q_t_t_async_8h.html#a0388b226a414b09fa733f6d65004ec32">MQTTAsync_connect</a>(client, &amp;conn_opts)) != <a class="code" href="_m_q_t_t_async_8h.html#afe0cffcce8efe25186f79c51ac44e16f">MQTTASYNC_SUCCESS</a>)</div><div class="line">        {</div><div class="line">                printf(<span class="stringliteral">&quot;Failed to start connect, return code %d\n&quot;</span>, rc);</div><div class="line">            finished = 1;</div><div class="line">        }</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">int</span> msgarrvd(<span class="keywordtype">void</span> *context, <span class="keywordtype">char</span> *topicName, <span class="keywordtype">int</span> topicLen, <a class="code" href="struct_m_q_t_t_async__message.html">MQTTAsync_message</a> *message)</div><div class="line">{</div><div class="line">    <span class="keywordtype">int</span> i;</div><div class="line">    <span class="keywordtype">char</span>* payloadptr;</div><div class="line"></div><div class="line">    printf(<span class="stringliteral">&quot;Message arrived\n&quot;</span>);</div><div class="line">    printf(<span class="stringliteral">&quot;     topic: %s\n&quot;</span>, topicName);</div><div class="line">    printf(<span class="stringliteral">&quot;   message: &quot;</span>);</div><div class="line"></div><div class="line">    payloadptr = message-&gt;<a class="code" href="struct_m_q_t_t_async__message.html#a9eff55064941fb604452abb0050ea99d">payload</a>;</div><div class="line">    <span class="keywordflow">for</span>(i=0; i&lt;message-&gt;<a class="code" href="struct_m_q_t_t_async__message.html#aa3cb44feb3ae6d11b3a4cad2d94cb33a">payloadlen</a>; i++)</div><div class="line">    {</div><div class="line">        putchar(*payloadptr++);</div><div class="line">    }</div><div class="line">    putchar(<span class="charliteral">&#39;\n&#39;</span>);</div><div class="line">    <a class="code" href="_m_q_t_t_async_8h.html#a9b45db63052fe29ab1fad22d2a00c91c">MQTTAsync_freeMessage</a>(&amp;message);</div><div class="line">    <a class="code" href="_m_q_t_t_async_8h.html#a2b836f58612a2c4627e40ae848da190d">MQTTAsync_free</a>(topicName);</div><div class="line">    <span class="keywordflow">return</span> 1;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> onDisconnect(<span class="keywordtype">void</span>* context, <a class="code" href="struct_m_q_t_t_async__success_data.html">MQTTAsync_successData</a>* response)</div><div class="line">{</div><div class="line">        printf(<span class="stringliteral">&quot;Successful disconnection\n&quot;</span>);</div><div class="line">        disc_finished = 1;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> onSubscribe(<span class="keywordtype">void</span>* context, <a class="code" href="struct_m_q_t_t_async__success_data.html">MQTTAsync_successData</a>* response)</div><div class="line">{</div><div class="line">        printf(<span class="stringliteral">&quot;Subscribe succeeded\n&quot;</span>);</div><div class="line">        subscribed = 1;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> onSubscribeFailure(<span class="keywordtype">void</span>* context, <a class="code" href="struct_m_q_t_t_async__failure_data.html">MQTTAsync_failureData</a>* response)</div><div class="line">{</div><div class="line">        printf(<span class="stringliteral">&quot;Subscribe failed, rc %d\n&quot;</span>, response ? response-&gt;<a class="code" href="struct_m_q_t_t_async__failure_data.html#a45a5b7c00a796a23f01673cef1dbe0a9">code</a> : 0);</div><div class="line">        finished = 1;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> onConnectFailure(<span class="keywordtype">void</span>* context, <a class="code" href="struct_m_q_t_t_async__failure_data.html">MQTTAsync_failureData</a>* response)</div><div class="line">{</div><div class="line">        printf(<span class="stringliteral">&quot;Connect failed, rc %d\n&quot;</span>, response ? response-&gt;<a class="code" href="struct_m_q_t_t_async__failure_data.html#a45a5b7c00a796a23f01673cef1dbe0a9">code</a> : 0);</div><div class="line">        finished = 1;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> onConnect(<span class="keywordtype">void</span>* context, <a class="code" href="struct_m_q_t_t_async__success_data.html">MQTTAsync_successData</a>* response)</div><div class="line">{</div><div class="line">        <a class="code" href="_m_q_t_t_async_8h.html#a0db1d736cdc0c864fe41abb3afd605bd">MQTTAsync</a> client = (<a class="code" href="_m_q_t_t_async_8h.html#a0db1d736cdc0c864fe41abb3afd605bd">MQTTAsync</a>)context;</div><div class="line">        <a class="code" href="struct_m_q_t_t_async__response_options.html">MQTTAsync_responseOptions</a> opts = <a class="code" href="_m_q_t_t_async_8h.html#a3f8b408243b5c2369bc9758f2edf0878">MQTTAsync_responseOptions_initializer</a>;</div><div class="line">        <a class="code" href="struct_m_q_t_t_async__message.html">MQTTAsync_message</a> pubmsg = <a class="code" href="_m_q_t_t_async_8h.html#a6a85061dadab532f28e96e5ab3c600e9">MQTTAsync_message_initializer</a>;</div><div class="line">        <span class="keywordtype">int</span> rc;</div><div class="line"></div><div class="line">        printf(<span class="stringliteral">&quot;Successful connection\n&quot;</span>);</div><div class="line"></div><div class="line">        printf(<span class="stringliteral">&quot;Subscribing to topic %s\nfor client %s using QoS%d\n\n&quot;</span></div><div class="line">           <span class="stringliteral">&quot;Press Q&lt;Enter&gt; to quit\n\n&quot;</span>, TOPIC, CLIENTID, QOS);</div><div class="line">        opts.<a class="code" href="struct_m_q_t_t_async__response_options.html#ac13fb68f736854fcab131b34756bfceb">onSuccess</a> = onSubscribe;</div><div class="line">        opts.<a class="code" href="struct_m_q_t_t_async__response_options.html#a09ce26d7cff24e14a6844eaae7b15290">onFailure</a> = onSubscribeFailure;</div><div class="line">        opts.<a class="code" href="struct_m_q_t_t_async__response_options.html#ae376f130b17d169ee51be68077a89ed0">context</a> = client;</div><div class="line"></div><div class="line">        deliveredtoken = 0;</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> ((rc = <a class="code" href="_m_q_t_t_async_8h.html#ae10bd009934b3bb4a9f4abae7424a611">MQTTAsync_subscribe</a>(client, TOPIC, QOS, &amp;opts)) != <a class="code" href="_m_q_t_t_async_8h.html#afe0cffcce8efe25186f79c51ac44e16f">MQTTASYNC_SUCCESS</a>)</div><div class="line">        {</div><div class="line">                printf(<span class="stringliteral">&quot;Failed to start subscribe, return code %d\n&quot;</span>, rc);</div><div class="line">                exit(EXIT_FAILURE);</div><div class="line">        }</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>* argv[])</div><div class="line">{</div><div class="line">        <a class="code" href="_m_q_t_t_async_8h.html#a0db1d736cdc0c864fe41abb3afd605bd">MQTTAsync</a> client;</div><div class="line">        <a class="code" href="struct_m_q_t_t_async__connect_options.html">MQTTAsync_connectOptions</a> conn_opts = <a class="code" href="_m_q_t_t_async_8h.html#ae18b51f22784a43803eb809d6a0c2492">MQTTAsync_connectOptions_initializer</a>;</div><div class="line">        <a class="code" href="struct_m_q_t_t_async__disconnect_options.html">MQTTAsync_disconnectOptions</a> disc_opts = <a class="code" href="_m_q_t_t_async_8h.html#a2fd5d6df31928ae468f3f2e522b9c707">MQTTAsync_disconnectOptions_initializer</a>;</div><div class="line">        <a class="code" href="struct_m_q_t_t_async__message.html">MQTTAsync_message</a> pubmsg = <a class="code" href="_m_q_t_t_async_8h.html#a6a85061dadab532f28e96e5ab3c600e9">MQTTAsync_message_initializer</a>;</div><div class="line">        <a class="code" href="_m_q_t_t_async_8h.html#a7ca6d2a1813f2bbd0bc3af2771e46ba4">MQTTAsync_token</a> token;</div><div class="line">        <span class="keywordtype">int</span> rc;</div><div class="line">        <span class="keywordtype">int</span> ch;</div><div class="line"></div><div class="line">        <a class="code" href="_m_q_t_t_async_8h.html#a5462c4618d0a229116db5fbadacf95d2">MQTTAsync_create</a>(&amp;client, ADDRESS, CLIENTID, <a class="code" href="_m_q_t_t_client_persistence_8h.html#ae01e089313a65ac4661ed216b6ac00fa">MQTTCLIENT_PERSISTENCE_NONE</a>, NULL);</div><div class="line"></div><div class="line">        <a class="code" href="_m_q_t_t_async_8h.html#ae9ae8d61023e7029ef5a19f5219c3599">MQTTAsync_setCallbacks</a>(client, NULL, connlost, msgarrvd, NULL);</div><div class="line"></div><div class="line">        conn_opts.<a class="code" href="struct_m_q_t_t_async__connect_options.html#ac8dd0930672a9c7d71fc645aa1f0521d">keepAliveInterval</a> = 20;</div><div class="line">        conn_opts.<a class="code" href="struct_m_q_t_t_async__connect_options.html#a036c36a2a4d3a3ffae9ab4dd8b3e7f7b">cleansession</a> = 1;</div><div class="line">        conn_opts.<a class="code" href="struct_m_q_t_t_async__connect_options.html#ac13fb68f736854fcab131b34756bfceb">onSuccess</a> = onConnect;</div><div class="line">        conn_opts.<a class="code" href="struct_m_q_t_t_async__connect_options.html#a09ce26d7cff24e14a6844eaae7b15290">onFailure</a> = onConnectFailure;</div><div class="line">        conn_opts.<a class="code" href="struct_m_q_t_t_async__connect_options.html#ae376f130b17d169ee51be68077a89ed0">context</a> = client;</div><div class="line">        <span class="keywordflow">if</span> ((rc = <a class="code" href="_m_q_t_t_async_8h.html#a0388b226a414b09fa733f6d65004ec32">MQTTAsync_connect</a>(client, &amp;conn_opts)) != <a class="code" href="_m_q_t_t_async_8h.html#afe0cffcce8efe25186f79c51ac44e16f">MQTTASYNC_SUCCESS</a>)</div><div class="line">        {</div><div class="line">                printf(<span class="stringliteral">&quot;Failed to start connect, return code %d\n&quot;</span>, rc);</div><div class="line">                exit(EXIT_FAILURE);</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="keywordflow">while</span>   (!subscribed)</div><div class="line"><span class="preprocessor">                #if defined(WIN32) || defined(WIN64)</span></div><div class="line">                        Sleep(100);</div><div class="line"><span class="preprocessor">                #else</span></div><div class="line">                        usleep(10000L);</div><div class="line"><span class="preprocessor">                #endif</span></div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> (finished)</div><div class="line">                <span class="keywordflow">goto</span> exit;</div><div class="line"></div><div class="line">        <span class="keywordflow">do</span></div><div class="line">        {</div><div class="line">                ch = getchar();</div><div class="line">        } <span class="keywordflow">while</span> (ch!=<span class="charliteral">&#39;Q&#39;</span> &amp;&amp; ch != <span class="charliteral">&#39;q&#39;</span>);</div><div class="line"></div><div class="line">        disc_opts.<a class="code" href="struct_m_q_t_t_async__disconnect_options.html#ac13fb68f736854fcab131b34756bfceb">onSuccess</a> = onDisconnect;</div><div class="line">        <span class="keywordflow">if</span> ((rc = <a class="code" href="_m_q_t_t_async_8h.html#adc69afa4725f8321bdaa5a05aec5cfd5">MQTTAsync_disconnect</a>(client, &amp;disc_opts)) != <a class="code" href="_m_q_t_t_async_8h.html#afe0cffcce8efe25186f79c51ac44e16f">MQTTASYNC_SUCCESS</a>)</div><div class="line">        {</div><div class="line">                printf(<span class="stringliteral">&quot;Failed to start disconnect, return code %d\n&quot;</span>, rc);</div><div class="line">                exit(EXIT_FAILURE);</div><div class="line">        }</div><div class="line">        <span class="keywordflow">while</span>   (!disc_finished)</div><div class="line"><span class="preprocessor">                #if defined(WIN32) || defined(WIN64)</span></div><div class="line">                        Sleep(100);</div><div class="line"><span class="preprocessor">                #else</span></div><div class="line">                        usleep(10000L);</div><div class="line"><span class="preprocessor">                #endif</span></div><div class="line"></div><div class="line">exit:</div><div class="line">        <a class="code" href="_m_q_t_t_async_8h.html#ad5562f9dc71fbd93d25ad20b328cb887">MQTTAsync_destroy</a>(&amp;client);</div><div class="line">        <span class="keywordflow">return</span> rc;</div><div class="line">}</div></div><!-- fragment --> </div></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Thu Sep 13 2018 13:40:20 for Paho Asynchronous MQTT C Client Library by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.14
</small></address>
</body>
</html>
